<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_NEWYEAR_WEEK.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_NEWYEAR, $login['staff_auth']);
$dao = new Class_M_NEWYEAR_WEEK;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	/*
	if ($_GET["y4"]){
		$forms  = $dao->getByYear( $_GET["y4"] );
	}else{
		$forms  = $dao->getByYear( date('Y') );
	}
	*/
	
	$phase = 'input';
	$forms  = $dao->get( 1 );
	if (empty($forms["year4"])) {
		$forms["year4"] = date('Y');
	}
	
	$smarty->assign('year4',  Class_PWD::get_years());
	$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
	$smarty->assign('week', $week );
	
}else{
	$forms = $_POST; 
	
	if (empty($forms["year4"])) {
		$forms["year4"] = date('Y');
	}
	
	$smarty->assign('year4',  Class_PWD::get_years());
	$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
	$smarty->assign('week', $week );
	
	if( $forms['mode'] == 'input' ){
		$phase  = "input" ;
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		
		
		for ($i = 1; $i < 4; $i ++){
		  for ($j=$i + 1 ; $j<=4; $j++) {
		  	$matr1 = "matrix_cd".$i;
		  	$matr2 = "matrix_cd".$j;
		  	if  ((!empty($forms[$matr1])) && (!empty($forms[$matr2])) ) {
		  		if ($forms[$matr1] == $forms[$matr2]) {
					$err->clear = false;
		  			$err_mes[$matr1]  = "<p class='error'>告知チラシ週名{$j}と重複しています </p>";
		  		}
		  	}  
		  }
		  
		}
		if( $err->clear ){
			try {
				$values = array();
				$values["year4"]         =  $forms["year4"];
				
				$values["matrix_cd1"] =  empty( $forms["matrix_cd1"] ) ? "0" : $forms["matrix_cd1"] ;
				$values["week1"]      =  $week[$values["matrix_cd1"]];
				
				$values["matrix_cd2"] =  empty( $forms["matrix_cd2"] ) ? "0" : $forms["matrix_cd2"] ;
				$values["week2"]      =  $week[$values["matrix_cd2"]];
				
				$values["matrix_cd3"] =  empty( $forms["matrix_cd3"] ) ? "0" : $forms["matrix_cd3"] ;
				$values["week3"]      =  $week[$values["matrix_cd3"]];
				
				$values["matrix_cd4"] =  empty( $forms["matrix_cd4"] ) ? "0" : $forms["matrix_cd4"] ;
				$values["week4"]      =  $week[$values["matrix_cd4"]];
				
				$dao->begin_trans() ;
				if ($dao->edit( 1, $values )) {
					if ($dao->update_newyear($values)) {
						$dao->commit_trans();
						$phase  = "complete" ;
						$smarty->assign('message', '更新が完了しました。');
					}else {
					$smarty->assign('message', '更新が失敗しました。');
					$dao->rollback_trans();
					}
					
				} else {
					$smarty->assign('message', '更新が失敗しました。');
					$dao->rollback_trans();
				}
			} catch (Exception $e) {
				$dao->rollback_trans();
				$smarty->assign('message', 'ＤＢエラーで失敗しました。');
			}
		}
	}
}	
		





if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mnewyear_week_form.html');
?>